package ru.averta.notes.domain;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Store;
import ru.averta.notes.domain.abstr.UserableObject;

import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;

@Entity
@Table(name = "Tag")
public class Tag extends UserableObject {

    @Field(store = Store.YES, index = Index.UN_TOKENIZED)
    @Column(name = "TITLE")
    private String title;

    @Column(name = "COUNT")
    private int count;

    @OneToMany(fetch = FetchType.LAZY, mappedBy="tag")
    private Collection<TagConnector> tagConnectors = new HashSet<TagConnector>();

    public Tag() {
    }

    public String getTitle() {
        return title;
    }

    public Tag(User user, String title) {
        this.setUser(user);
        this.title = title.toLowerCase();
    }

    public void increaseCount() {
        count++;
    }

    public void decreaseCount() {
        count--;
    }

    public int getCount() {
        return count;
    }

    public Collection<TagConnector> getTagConnectors() {
        return tagConnectors;
    }

    public boolean equalsTitle(String title) {
        return title.equalsIgnoreCase(this.title);
    }

}
